package com.example.fw;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.ContactData;
import com.example.utils.SortedListOf;

public class ContactHelper extends HelperBase {

	public static boolean CREATION = true;
	public static boolean MODIFICATION = false;
		
	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}

	private SortedListOf<ContactData> cachedContacts;
	
	public SortedListOf<ContactData> getContacts() {
		if (cachedContacts == null) {
			rebuildCache();
		}
		return cachedContacts;
	}
	
	private void rebuildCache() {
		cachedContacts = new SortedListOf<ContactData>();
		
		manager.navigateTo().mainPage();
		List<WebElement> checkboxes = driver.findElements(By.xpath("//input[@name = 'selected[]']"));
		for (WebElement checkbox : checkboxes) {
		  String title = checkbox.getAttribute("title");
		  if (title.equals("Select ( )"))
		  	{
			  String firstname = title.substring("Select (".length(), title.length() - " )". length()); 
			  cachedContacts.add(new ContactData().withFirstName(firstname));
		  	}
		  else
		  	{
			  String firstname = title.substring("Select (".length(), title.length() - " Sparrow)". length()); 
			  cachedContacts.add(new ContactData().withFirstName(firstname));
		  	}
		 }
	}

	public ContactHelper createContact(ContactData contact, boolean CREATION) {
		manager.navigateTo().mainPage();
		gotoNewContact();
		fillContactForm(contact, CREATION);
		submitNewContact();
		gotoHomepage();
		rebuildCache();
		return this;
	}
	
	public ContactHelper deleteContact(int index) {
		initContactEditor(index);
		submitContactDeletion();
		gotoHomepage();
		rebuildCache();
		return this;
	}

	public ContactHelper modifyContact(int index, ContactData contact,
		boolean MODIFICATION) {
		initContactEditor(index);
		fillContactForm(contact, MODIFICATION);
		submitContactModification();
		gotoHomepage();
		rebuildCache();
		return this;
	}

	//--------------------------------------------------------------------------------------
	
	private void submitContactDeletion() {
		click(By.xpath("//input[@value = 'Delete']"));
		cachedContacts = null;
	}

	public ContactHelper gotoNewContact() {
		manager.navigateTo().mainPage();
		click(By.linkText("add new"));
		return this;
	}

	public ContactHelper fillContactForm(ContactData contact, boolean formType) {
		type(By.name("firstname"), contact.getFirstname());
		type(By.name("lastname"), contact.getSecondname());
		type(By.name("address"), contact.getAddress());
		type(By.name("home"), contact.getHomephone());
		type(By.name("mobile"), contact.getMobilephone());
		type(By.name("work"), contact.getWorkphone());
		type(By.name("email"), contact.getEmail());
		type(By.name("email2"), contact.getEmail2());
		selectByText(By.name("bday"), contact.getBday());
		selectByText(By.name("bmonth"), contact.getBmonth());
		type(By.name("byear"), contact.getByear());
		if (formType == CREATION) {
			selectByText(By.name("new_group"), contact.getGroup());
		} else {
			if (driver.findElements(By.name("new_group")).size() != 0) {
				throw new Error("Group selector exists in contact modification form");
			}
		}
		type(By.name("address2"), contact.getAddress2());
		type(By.name("phone2"), contact.getPhone2());
		return this;
	}

	public ContactHelper submitNewContact() {
		click(By.name("submit"));
		cachedContacts = null;
		return this;
	}

	public ContactHelper gotoHomepage() {
		click(By.linkText("home page"));
		return this;
	}

	public ContactHelper initContactEditor(int index) {
		manager.navigateTo().mainPage();
		click(By.xpath("//tr[" + (index+1) + "]/td[7]/a/img"));
		return this;
	}

	public ContactHelper submitContactModification() {
		click(By.xpath("//input[@value = 'Update']"));
		cachedContacts = null;
		return this;
		}
}
